//
//  SearchDetailViewController.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/24.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "SearchDetailViewController.h"

//跳转界面
#import "GoodsDetailViewController.h"

//自定义视图
#import "SearchTopView.h"
#import "AllBtnClickedView.h"

//自定义cell
#import "SearchGoodCollectionViewCell.h"

@interface SearchDetailViewController ()<clickedCellTitleDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,keyBoardClickedDelegate>
@property(nonatomic,strong)SearchTopView *topView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UILabel *leftLabel;//综合
@property (weak, nonatomic) IBOutlet UILabel *midLabel;//销量
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;//价格
@property (weak, nonatomic) IBOutlet UIImageView *priceImg;//价格图标


//判断价格状态
@property(nonatomic,assign)NSInteger flag;

//适配
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHig;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftLabelTrailing;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightLabelLeading;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonWid;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftButtonTrailing;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightButtonLeading;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ImgHig;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgWid;


@end

@implementation SearchDetailViewController


static NSString *SearchGoodCollectionViewCellIden = @"SearchGoodCollectionViewCellIden";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.topViewHig.constant = 41 * FitH;
    
    self.buttonWid.constant = 41 * FitW;
    
    self.leftButtonTrailing.constant = 80 * FitW;
    self.rightButtonLeading.constant = 80 * FitW;
    
    
    self.imgWid.constant = 7 * FitW;
    self.ImgHig.constant = 12 * FitH;
    
    self.leftLabelTrailing.constant = 91 * FitW;
    self.rightLabelLeading.constant = 91 * FitW;
    
    self.leftLabel.font = [UIFont systemFontOfSize:16 * FitH];
    self.midLabel.font = [UIFont systemFontOfSize:16 * FitH];
    self.rightLabel.font = [UIFont systemFontOfSize:16 * FitH];
    
    [self load_UI];

}

#pragma mark ------- load_UI
-(void)load_UI
{
    self.flag = 0;
    
    [self topView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"SearchGoodCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:SearchGoodCollectionViewCellIden];
}

#pragma mark ----- collectionView delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 30;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(170 * FitW, 224 * FitH);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(12 * FitH, 10 * FitW, 12 * FitH, 10 * FitW);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SearchGoodCollectionViewCellIden forIndexPath:indexPath];
    cell.addCartButtonHandler = ^{
    
        DLog(@"加入购物车-----搜索界面");
    };
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsDetailViewController *gvc = [[GoodsDetailViewController alloc]initWithNibName:@"GoodsDetailViewController" bundle:nil];
    [self.navigationController pushViewController:gvc animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)clickedCellChangeTitle:(NSString *)titleString
{
    [self.topView.mySearchBar resignFirstResponder];
    [self.topView.mySearchBar.allButton setHighlighted:NO];
    self.topView.mySearchBar.allButton.titleLabel.text = titleString;
    [_topView.mySearchBar.allButton setTitle:titleString forState:UIControlStateNormal];
    NSInteger length                                   = titleString.length;
    CGRect rx                                          = CGRectMake( 0,0,(30 + length * 15 ) * FitW, 30 * FitH);
    _topView.mySearchBar.intputView.frame              = rx;
    _topView.mySearchBar.leftView                      = _topView.mySearchBar.intputView;
    [self.topView.mySearchBar becomeFirstResponder];
}

#pragma mark ------- private Method
-(void)clickedAction:(NSString *)searchText
{
    DLog(@"点击键盘搜索键代理事件----------%@",searchText);
    //判断输入框是否为空或者空格键
    NSString *textFieldText = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if ([textFieldText length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入您想搜索的关键字哦" delegate:self cancelButtonTitle:@"知道啦" otherButtonTitles:nil, nil];
        [alert show];
    }
    else
    {
        //重新请求网络字段，清空本地数组数据，重新赋值，刷新collectionView
        DLog(@"搜索加载当前字段的商品");
        [self.topView.mySearchBar resignFirstResponder];//回收键盘
    }

}

#pragma mark ------- private Method
- (IBAction)topButtonAction:(UIButton *)sender {
    
    switch (sender.tag)
    {
        case 0:
            DLog(@"综合");
            self.leftLabel.textColor = [UIColor colorWithHexString:@"ff5f7b"];
            self.midLabel.textColor = [UIColor blackColor];
            self.rightLabel.textColor = [UIColor blackColor];
            break;
        case 1:
            DLog(@"销量");
            self.rightLabel.textColor = [UIColor blackColor];
            self.leftLabel.textColor = [UIColor blackColor];
            self.midLabel.textColor = [UIColor colorWithHexString:@"ff5f7b"];
            break;
        case 2:
            DLog(@"价格");
            self.leftLabel.textColor = [UIColor blackColor];
            self.midLabel.textColor = [UIColor blackColor];
            self.rightLabel.textColor = [UIColor colorWithHexString:@"ff5f7b"];
            if (self.flag == 0)
            {
                self.priceImg.image = LoadImage(@"priceDown");
                self.flag = 1;
            }
            else
            {
                self.priceImg.image = LoadImage(@"priceUp");
                self.flag = 0;
            }
            break;
        default:
            break;
    }
}



#pragma mark -------- lazy loading
-(SearchTopView *)topView
{
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[SearchTopView alloc]init];
        //        _topView.mySearchBar.isFirstResponder = YES;
//                _topView.backgroundColor = [UIColor redColor];
        _topView.mySearchBar.text = self.searchText;
        _topView.mySearchBar.keyBoardDelegate = self;
        _topView.mySearchBar.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.height.mas_offset(44);
        }];
        
        _topView.backButtonClickedHandler = ^(NSInteger buttonTag){
            
            if (buttonTag == 0)
            {
                [weakself.topView.mySearchBar resignFirstResponder];//回收键盘
                
                [weakself.navigationController popViewControllerAnimated:YES];
            }
            else if(buttonTag == 1)
            {
                DLog(@"搜索一下~~~~~~~~%@",weakself.searchText);
                
                
                
                [weakself.topView.mySearchBar resignFirstResponder];//回收键盘

            }
            else
            {
                DLog(@"全部~~~~~~~~~~~~~~");
                AllBtnClickedView *clickedView = [AllBtnClickedView getInstance];
                clickedView.layer.cornerRadius = 5 * FitH;
                clickedView.backgroundColor = [UIColor blackColor];
                clickedView.alpha = .7;
                clickedView.clikedCellDelegate = weakself;
                [weakself.view addSubview:clickedView];
                [clickedView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(weakself.topView.mas_bottom).offset(5 * FitH);
                    make.left.equalTo(weakself.view.mas_left).offset(28 * FitW);
                    make.size.mas_offset(CGSizeMake(92 * FitW, 144 * FitH));
                }];
                clickedView.hidden = NO;
                
            }
        };
        
    }
    return _topView;
}





@end
